#include "Arduino.h"
#include "OneButton.h"
#include "WiFi.h"
#include "lvgl.h"
#include <TFT_eSPI.h>

#include "lvgl_gui.h"
#include "drv_cfg_tool.h"
#include "dd.h"
#include "drv_wifi.h"

#define SCREEN_WIDTH         128
#define SCREEN_WIDTH         128

#define WIFI_SSID            "HUAWEI_WiFi"
#define WIFI_PASSWORLD       "827289014"

#define PIN_BAT_VOLT         4
#define PIN_BTN_L            0
#define PIN_BTN_R            47
#define PIN_LCD_BL           10

static lv_disp_draw_buf_t draw_buf;
static lv_color_t buf[SCREEN_WIDTH * SCREEN_WIDTH];

OneButton btn_left(PIN_BTN_L, true);
OneButton btn_right(PIN_BTN_R, true);

TFT_eSPI tft =  TFT_eSPI();

static uint8_t page_num = 0;

#define LV_DELAY(x)                                                            \
  do {                                                                         \
    uint32_t t = x;                                                            \
    while (t--) {                                                              \
      lv_timer_handler();                                                      \
      delay(1);                                                                \
    }                                                                          \
  } while (0);

void disp_flush(lv_disp_drv_t *disp, const lv_area_t *area,
                lv_color_t *color_p)
{
  uint32_t w = ( area->x2 - area->x1 + 1 );
  uint32_t h = ( area->y2 - area->y1 + 1 );

  tft.startWrite();
  tft.setAddrWindow( area->x1, area->y1, w, h );
  tft.pushColors( ( uint16_t * )&color_p->full, w * h, false );
  tft.endWrite();

  lv_disp_flush_ready( disp );
}

void gui_next_page(void)
{
  page_num++;
  if ( page_num >= 2) {
    page_num = 0;
  }
  gui_switch_page(page_num);
}

void gui_previous_page(void)
{
  page_num--;
  if ( page_num >= 2) {
    page_num = 0;
  }
  gui_switch_page(page_num);
}

void go_to_sleep(void)
{
  Serial.println("sleep");

  // High level off the backlight
  digitalWrite(PIN_LCD_BL, HIGH);
  //Enable backlight hold on high level
  gpio_hold_en((gpio_num_t)PIN_LCD_BL);

  //IO47 cannot be set to the source of the awakening,
  //and can only be set to GPIO0 as the keys to wake the source
#if ESP_IDF_VERSION >=  ESP_IDF_VERSION_VAL(4,4,6)
  esp_sleep_enable_ext1_wakeup(GPIO_SEL_0, ESP_EXT1_WAKEUP_ANY_LOW);
#else
  esp_sleep_enable_ext1_wakeup(GPIO_SEL_0, ESP_EXT1_WAKEUP_ALL_LOW);
#endif

  esp_deep_sleep_start();
}

void setup()
{
  Serial.begin(115200);
  Serial.println("Hello T-QT");

  //Disable backlight hold on
  gpio_hold_dis((gpio_num_t)PIN_LCD_BL);

  //! No need to initialize the backlight here. TFT_ESPI has initialized it and opened the backlight after calling Begin
  // pinMode(PIN_LCD_BL, OUTPUT);
  // digitalWrite(PIN_LCD_BL, HIGH);

  pinMode(PIN_BAT_VOLT, ANALOG);

  analogReadResolution(12);

  tft.begin();
  tft.setRotation(2);
  tft.setTextFont(1);
  tft.setTextColor(TFT_GREEN, TFT_BLACK);

  tft.fillScreen(TFT_BLACK);
  tft.println("System start");

  if (drv_cfg_tool_init()) {
    tft.println("cfg init success");
  }
  else {
    tft.println("cfg init failed");
    while(true){delay(1);};
  }

  if (cfg_read_config_from_spiffs()) {
    tft.println("cfg read success");
  }
  else {
    tft.println("cfg read failed");
    while(true){delay(1);};
  }
  
  tft.print("WiFI_MODE:");
  tft.println(pWiFi_Config->WiFi_mode);
  tft.print("SSID:");
  tft.println(pWiFi_Config->client_ssid);
  tft.print("PASSWORD:");
  tft.println(pWiFi_Config->client_password);

  tft.print("MQTT_URL:");
  tft.println(pMQTT_Config->url);
  tft.print("MQTT_PORT:");
  tft.println(pMQTT_Config->port);
  tft.print("USERNAME:");
  tft.println(pMQTT_Config->username);
  tft.print("PASSWORD:");
  tft.println(pMQTT_Config->password);

  drv_wifi_init();
  tft.println("WiFi initialized");

  delay(2000);


  //若没有使用到LVGL注释此行后面
  // btn_left.attachClick(gui_previous_page);
  // btn_right.attachClick(gui_next_page);

  // lv_init();
  // lv_disp_draw_buf_init(&draw_buf, buf, NULL, SCREEN_WIDTH * SCREEN_WIDTH);

  // /*Initialize the display*/
  // static lv_disp_drv_t disp_drv;
  // lv_disp_drv_init(&disp_drv);
  // /*Change the following line to your display resolution*/
  // disp_drv.hor_res = SCREEN_WIDTH;
  // disp_drv.ver_res = SCREEN_WIDTH;
  // disp_drv.flush_cb = disp_flush;
  // disp_drv.draw_buf = &draw_buf;
  // lv_disp_drv_register(&disp_drv);

  // gui_init();
}

void loop()
{
  //lv_timer_handler();
  //btn_left.tick();
  //btn_right.tick();
  delay(1);
}
